package com.example.jobdemo.controller;

import com.example.jobdemo.entity.UserComment;
import com.example.jobdemo.mapper.UserCommentMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("comment")
public class CommentController {

    @Autowired
    private UserCommentMapper userCommentMapper;

    @RequestMapping("page")
    public Map<String, Object> page(@RequestBody UserComment comment) {
        Map<String, Object> map = new HashMap<>();
        Integer count = userCommentMapper.selectCommentCount();
        int pageIndex = (comment.getPageIndex() -1)* comment.getPageSize();
        List<UserComment> companyList = userCommentMapper.selectCommentList(pageIndex,comment.getPageSize());
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("data", companyList);
        map.put("count",count);
        return map;
    }

    @RequestMapping("insert")
    public Map<String, Object> insert(@RequestBody UserComment comment) {
        userCommentMapper.insertSelective(comment);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("update")
    public Map<String, Object> update(@RequestBody UserComment comment) {
        userCommentMapper.updateByPrimaryKeySelective(comment);
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }

    @RequestMapping("delete")
    public Map<String, Object> delete(@RequestBody UserComment comment) {
        userCommentMapper.deleteByPrimaryKey(comment.getUserCommentId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "成功");
        return map;
    }
}
